package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.LabMainInfoRlm;
import cn.stylefeng.guns.modular.biz.enums.LabMainInfoRlmExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.LabMainInfoRlmMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.LabMainInfoRlmRequest;
import cn.stylefeng.guns.modular.biz.service.LabMainInfoRlmService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;

/**
 * MES煤的入炉煤质检数据业务实现层
 *
 * @author 张朋飞
 * @since 2024/10/31 17:00
 */
@Service
public class LabMainInfoRlmServiceImpl extends ServiceImpl<LabMainInfoRlmMapper, LabMainInfoRlm> implements LabMainInfoRlmService {

	@Override
    public void add(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        LabMainInfoRlm labMainInfoRlm = new LabMainInfoRlm();
        BeanUtil.copyProperties(labMainInfoRlmRequest, labMainInfoRlm);
        this.save(labMainInfoRlm);
    }

    @Override
    public void del(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        LabMainInfoRlm labMainInfoRlm = this.queryLabMainInfoRlm(labMainInfoRlmRequest);
        this.removeById(labMainInfoRlm.getLmiId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        this.removeByIds(labMainInfoRlmRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        LabMainInfoRlm labMainInfoRlm = this.queryLabMainInfoRlm(labMainInfoRlmRequest);
        BeanUtil.copyProperties(labMainInfoRlmRequest, labMainInfoRlm);
        this.updateById(labMainInfoRlm);
    }

    @Override
    public LabMainInfoRlm detail(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        return this.queryLabMainInfoRlm(labMainInfoRlmRequest);
    }

    @Override
    public PageResult<LabMainInfoRlm> findPage(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        LambdaQueryWrapper<LabMainInfoRlm> wrapper = createWrapper(labMainInfoRlmRequest);
        Page<LabMainInfoRlm> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<LabMainInfoRlm> findList(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        LambdaQueryWrapper<LabMainInfoRlm> wrapper = this.createWrapper(labMainInfoRlmRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/10/31 17:00
     */
    private LabMainInfoRlm queryLabMainInfoRlm(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        LabMainInfoRlm labMainInfoRlm = this.getById(labMainInfoRlmRequest.getLmiId());
        if (ObjectUtil.isEmpty(labMainInfoRlm)) {
            throw new ServiceException(LabMainInfoRlmExceptionEnum.LAB_MAIN_INFO_RLM_NOT_EXISTED);
        }
        return labMainInfoRlm;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/10/31 17:00
     */
    private LambdaQueryWrapper<LabMainInfoRlm> createWrapper(LabMainInfoRlmRequest labMainInfoRlmRequest) {
        LambdaQueryWrapper<LabMainInfoRlm> queryWrapper = new LambdaQueryWrapper<>();

        Long lmiId = labMainInfoRlmRequest.getLmiId();
        queryWrapper.eq(ObjectUtil.isNotNull(lmiId), LabMainInfoRlm::getLmiId, lmiId);

        String sampleId = labMainInfoRlmRequest.getSampleId();
        queryWrapper.like(ObjectUtil.isNotEmpty(sampleId), LabMainInfoRlm::getSampleId, sampleId);

        String mt = labMainInfoRlmRequest.getMt();
        queryWrapper.like(ObjectUtil.isNotEmpty(mt), LabMainInfoRlm::getMt, mt);

        String ad = labMainInfoRlmRequest.getAd();
        queryWrapper.like(ObjectUtil.isNotEmpty(ad), LabMainInfoRlm::getAd, ad);

        String vd = labMainInfoRlmRequest.getVd();
        queryWrapper.like(ObjectUtil.isNotEmpty(vd), LabMainInfoRlm::getVd, vd);

        String mad = labMainInfoRlmRequest.getMad();
        queryWrapper.like(ObjectUtil.isNotEmpty(mad), LabMainInfoRlm::getMad, mad);

        String qnetar = labMainInfoRlmRequest.getQnetar();
        queryWrapper.like(ObjectUtil.isNotEmpty(qnetar), LabMainInfoRlm::getQnetar, qnetar);

        String std = labMainInfoRlmRequest.getStd();
        queryWrapper.like(ObjectUtil.isNotEmpty(std), LabMainInfoRlm::getStd, std);

        String hgi = labMainInfoRlmRequest.getHgi();
        queryWrapper.like(ObjectUtil.isNotEmpty(hgi), LabMainInfoRlm::getHgi, hgi);

        String cri = labMainInfoRlmRequest.getCri();
        queryWrapper.like(ObjectUtil.isNotEmpty(cri), LabMainInfoRlm::getCri, cri);

        String ci = labMainInfoRlmRequest.getCi();
        queryWrapper.like(ObjectUtil.isNotEmpty(ci), LabMainInfoRlm::getCi, ci);

        String car = labMainInfoRlmRequest.getCar();
        queryWrapper.like(ObjectUtil.isNotEmpty(car), LabMainInfoRlm::getCar, car);

        String had = labMainInfoRlmRequest.getHad();
        queryWrapper.like(ObjectUtil.isNotEmpty(had), LabMainInfoRlm::getHad, had);

        String qgrd = labMainInfoRlmRequest.getQgrd();
        queryWrapper.like(ObjectUtil.isNotEmpty(qgrd), LabMainInfoRlm::getQgrd, qgrd);


        return queryWrapper;
    }

}
